from django.db import models


# Create your models here.

class UserInfo(models.Model):
    username = models.CharField(max_length=32, verbose_name='工程师ID', db_index=True, null=True, blank=True)  # 允许为空
    password = models.CharField(max_length=32, verbose_name='密码')
    email = models.EmailField(verbose_name='邮箱', unique=True)  # 移除null=True，确保邮箱必填
    full_name = models.CharField(max_length=64, verbose_name='姓名', null=True)  # 允许姓名字段为空
    department = models.CharField(max_length=64, verbose_name='部门', null=True)  # 允许部门字段为空
    role_choices = (
        (1, "管理员"),
        (2, "用户")
    )
    # 修改字段类型为IntegerField以匹配choices的整数键
    role = models.IntegerField(verbose_name="角色", choices=role_choices, default=2)
    # 修改证书URL字段为型号权限字段
    kfv_cert_permission = models.BooleanField(verbose_name='KFV权限', default=False)
    kfc_cert_permission = models.BooleanField(verbose_name='KFC权限', default=False)
    dwy_cert_permission = models.BooleanField(verbose_name='DWY权限', default=False)
    ph_cert_permission = models.BooleanField(verbose_name='PH权限', default=False)
    hma_cert_permission = models.BooleanField(verbose_name='HMA权限', default=False)
    lz_cert_permission = models.BooleanField(verbose_name='LZ权限', default=False)
    ddl_cert_permission = models.BooleanField(verbose_name='DDL权限', default=False)
    def __str__(self):
        # 修改为优先显示邮箱，如果邮箱不存在则显示username
        return self.email if self.email else (self.username if self.username else "未知用户")


class ToolInfo(models.Model):
    title = models.CharField(max_length=64, verbose_name='工具名称')
    tool_number = models.CharField(max_length=64, verbose_name='编号')
    range = models.CharField(max_length=64, verbose_name='测量范围')
    certificate_number = models.CharField(max_length=64, verbose_name='证书编号')
    end_date = models.CharField(max_length=64, verbose_name='有效期')
    cal_jigou = models.CharField(max_length=64, verbose_name='校准机构')
    property = models.CharField(max_length=64, verbose_name='计量特性')
    base_info = models.CharField(max_length=32)
    # type_choices = (
    #     (1, "KFC"),
    #     (2, "PH"),
    #     (3, "DWY"),
    #     (4, "KFV"),
    #     (5, "HMA"),
    #     (6, "LZ"),
    #     (7, "DDL"),
        
    # )
    type = models.CharField(max_length=64, verbose_name='适用仪器')

    def __str__(self):
        return self.title


class Kfv(models.Model):
    all_data = models.JSONField(default=dict)
    date = models.CharField(max_length=64, default=None)
    name = models.CharField(max_length=64)
    c_number = models.CharField(max_length=32)
    customer = models.CharField(max_length=64)
    snr = models.CharField(max_length=64)
    status_choice = (
        (1, "已同步"),
        (2, "未同步")
    )
    status = models.IntegerField(choices=status_choice, default=2)
    user = models.CharField(max_length=32, null=True)
    approve_choice = (
        (1, "未审批"),
        (2, "已审批"),
        (3, "撤回"),
        (4, "校准废弃"),
    )
    approve = models.IntegerField(choices=approve_choice, default=1)
    type = models.CharField(max_length=32, default='KFV')
    version = models.IntegerField(default=1)  # 新增版本信息字段，默认值为1
    download_choice = (
        (1, "未下载"),
        (2, "已下载"),
    )
    download = models.IntegerField(choices=download_choice, default=1)
    comments = models.TextField(null=True)


class Kfc(models.Model):
    all_data = models.JSONField(default=dict)
    date = models.CharField(max_length=64, default=None)
    name = models.CharField(max_length=64)
    c_number = models.CharField(max_length=32)
    customer = models.CharField(max_length=64)
    snr = models.CharField(max_length=64)
    status_choice = (
        (1, "已同步"),
        (2, "未同步")
    )
    status = models.IntegerField(choices=status_choice, default=2)
    user = models.CharField(max_length=32, null=True)
    approve_choice = (
        (1, "未审批"),
        (2, "已审批"),
        (3, "撤回"),
        (4, "校准废弃"),
    )
    approve = models.IntegerField(choices=approve_choice, default=1)
    type = models.CharField(max_length=32, default='KFC')
    version = models.IntegerField(default=1)  # 新增版本信息字段，默认值为1
    download_choice = (
        (1, "未下载"),
        (2, "已下载"),
    )
    download = models.IntegerField(choices=download_choice, default=1)
    comments = models.TextField(null=True)


class Dwy(models.Model):
    all_data = models.JSONField(default=dict)
    date = models.CharField(max_length=64, default=None)
    type = models.CharField(max_length=32, default='DWY')
    c_number = models.CharField(max_length=32)
    customer = models.CharField(max_length=64)
    snr = models.CharField(max_length=64)
    status_choice = (
        (1, "已同步"),
        (2, "未同步")
    )
    status = models.IntegerField(choices=status_choice, default=2)

    approve_choice = (
        (1, "未审批"),
        (2, "已审批"),
        (3, "撤回"),
        (4, "校准废弃")
    )
    approve = models.IntegerField(choices=approve_choice, default=1)
    user = models.CharField(max_length=32, null=True)

    version = models.IntegerField(default=1)  # 新增版本信息字段，默认值为1
    download_choice = (
        (1, "未下载"),
        (2, "已下载"),
    )
    download = models.IntegerField(choices=download_choice, default=1)
    comments = models.TextField(null=True)


class Ph(models.Model):
    all_data = models.JSONField(default=dict)
    date = models.CharField(max_length=64, default=None)
    name = models.CharField(max_length=64)
    c_number = models.CharField(max_length=32)
    customer = models.CharField(max_length=64)
    snr = models.CharField(max_length=64)
    status_choice = (
        (1, "已同步"),
        (2, "未同步")
    )
    status = models.IntegerField(choices=status_choice, default=2)
    user = models.CharField(max_length=32, null=True)
    approve_choice = (
        (1, "未审批"),
        (2, "已审批"),
        (3, "撤回"),
        (4, "校准废弃"),
    )
    approve = models.IntegerField(choices=approve_choice, default=1)
    type = models.CharField(max_length=32, default='Ph')
    version = models.IntegerField(default=1)  # 新增版本信息字段，默认值为1
    download_choice = (
        (1, "未下载"),
        (2, "已下载"),
    )
    download = models.IntegerField(choices=download_choice, default=1)
    comments = models.TextField(null=True)



class Hma(models.Model):
    all_data = models.JSONField(default=dict)
    date = models.CharField(max_length=64, default=None)
    name = models.CharField(max_length=64)
    c_number = models.CharField(max_length=32)
    customer = models.CharField(max_length=64)
    snr = models.CharField(max_length=64)
    status_choice = (
        (1, "已同步"),
        (2, "未同步")
    )
    status = models.IntegerField(choices=status_choice, default=2)
    user = models.CharField(max_length=32, null=True)
    approve_choice = (
        (1, "未审批"),
        (2, "已审批"),
        (3, "撤回"),
        (4, "校准废弃"),
    )
    approve = models.IntegerField(choices=approve_choice, default=1)
    type = models.CharField(max_length=32, default='HMA')
    version = models.IntegerField(default=1)  # 新增版本信息字段，默认值为1
    download_choice = (
        (1, "未下载"),
        (2, "已下载"),
    )
    download = models.IntegerField(choices=download_choice, default=1)
    comments = models.TextField(null=True)


class Lz(models.Model):
    all_data = models.JSONField(default=dict)
    date = models.CharField(max_length=64, default=None)
    name = models.CharField(max_length=64)
    c_number = models.CharField(max_length=32)
    customer = models.CharField(max_length=64)
    snr = models.CharField(max_length=64)
    status_choice = (
        (1, "已同步"),
        (2, "未同步")
    )
    status = models.IntegerField(choices=status_choice, default=2)
    user = models.CharField(max_length=32, null=True)
    approve_choice = (
        (1, "未审批"),
        (2, "已审批"),
        (3, "撤回"),
        (4, "校准废弃"),
    )
    approve = models.IntegerField(choices=approve_choice, default=1)
    type = models.CharField(max_length=32, default='LZ')
    version = models.IntegerField(default=1)  # 新增版本信息字段，默认值为1
    download_choice = (
        (1, "未下载"),
        (2, "已下载"),
    )
    download = models.IntegerField(choices=download_choice, default=1)
    comments = models.TextField(null=True)

class Ddl(models.Model):
    all_data = models.JSONField(default=dict)
    date = models.CharField(max_length=64, default=None)
    name = models.CharField(max_length=64)
    c_number = models.CharField(max_length=32)
    customer = models.CharField(max_length=64)
    snr = models.CharField(max_length=64)
    status_choice = (
        (1, "已同步"),
        (2, "未同步")
    )
    status = models.IntegerField(choices=status_choice, default=2)
    user = models.CharField(max_length=32, null=True)
    approve_choice = (
        (1, "未审批"),
        (2, "已审批"),
        (3, "撤回"),
        (4, "校准废弃"),
    )
    approve = models.IntegerField(choices=approve_choice, default=1)
    type = models.CharField(max_length=32, default='DDL')
    version = models.IntegerField(default=1)  # 新增版本信息字段，默认值为1
    download_choice = (
        (1, "未下载"),
        (2, "已下载"),
    )
    download = models.IntegerField(choices=download_choice, default=1)
    comments = models.TextField(null=True)


